<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Vinkla\Hashids\Facades\Hashids;

class PostInfoResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        
        $user = \Auth::user();

        
        $strw = mb_strwidth(trim($this->body), 'UTF-8');
        $this->user->name = $this->user->name?$this->user->name:'游客'.$this->user->id;
        $this->user->avatar = $this->user->avatar?$this->user->avatar:'';
        $body = $strw>300?str_limit( trim($this->body),190 ): trim($this->body);
        $allbody = $strw>300?trim($this->body):'';
        $showall = $strw>300?1:0;

        return [
            'id'   => (string)$this->id,
            'user_id' => $this->user_id,
            'user' => $this->user,
            'me' => $user, // 用户自己(当前访问用户)
            'category' => $this->category,
            'body' => $body,
            'allbody' =>$allbody ,
            'showall' =>$showall,
            'isshowall'=>0,
            'video_mode'=>config('cms.video_mode'),
            'video'=>$this->video, // video
            'vid'=>$this->getQVideoVid($this->video), // 转换成腾讯视频vid
            'rank'  => $this->rank,
            'images'  => $this->images && $this->images[0]?$this->images:[],
            'real_images' =>$this->getRealImages(),
            'liked'  => $this->liked, //点赞数
            'userliked'=> config('cms.allow_interaction') && $user && $user->hasLiked($this->resource), // 已点赞
            'allow_interaction'=>config('cms.allow_interaction'), // 加载互动版块
            'commented'  => config('cms.use_comment_component')?$this->commented:-1, //评论数
            'share_title'=>str_limit($this->body,64), //从正文开始截取64个字
            'share_cover'=> $this->images && $this->images[0]?$this->images[0]:'',
            'time' => $this->created_at->from(),
        ];

    }

    // 获取原图
    private function getRealImages(){
        $images = $this->images && $this->images[0]?$this->images:[];
        foreach($images as $k=>$v){
            if(preg_match("/\/(\d+)\//u",$v,$res)){
                $images[$k] = strtr($v,['.com/'=>'.com/'.$this->user_id.'/'.Hashids::encode($this->user_id,intval($res[1])).'/']);
            }
        }
        return $images;
    }

    
    /**
     * 获取腾讯视频VID  todo 未测试各种链接的可能性
     * @param $url
     * @return string|void
     */
    public function getQVideoVid($url)
    {
        // 
        
        if (empty($url)  ) {
            return;
        }

        if(str_contains($url, 'v.qq.com/x/cover')){
            if(preg_match('/\/(\w+).html/',$url,$res)){

             return $res[1];
            }
        }
        if(str_contains($url, 'v.qq.com/x/page')){
            if(preg_match('/\/(\w+).html/',$url,$res)){

             return $res[1];
            }
        }
        
        if(str_contains($url, 'v.qq.com') ) { // 'v.qq.com/iframe/' 
           
            // 如果有转码符，替换掉
            // $url = strtr($url,['&amp;'=>'&']);
            $params = parse_url($url);
            parse_str($params['query'],$res);
            if(isset($res['vid'])) return $res['vid'];
            if(isset($res['amp;vid'])) return $res['amp;vid'];
            return;
        }
        return $url;
    }
}
